//
// Created by 常磊 on 2022/4/9.
//

import UIKit


/// 启动页
class LaunchController: BaseController {
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let scrollEdgeAppearance = scrollEdgeAppearance
        scrollEdgeAppearance?.configureWithTransparentBackground()
        navigationItem.scrollEdgeAppearance = scrollEdgeAppearance
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let uiLabel = UILabel()
        uiLabel.text = NSLocalizedString("appName", comment: "")
        uiLabel.font = .systemFont(ofSize: 27)
        uiLabel.textAlignment = .center
        view.addSubview(uiLabel)
        view.backgroundColor = .systemBackground

        uiLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) { [self] in
            navigationController?.delegate = self
            navigationController?.setViewControllers([HomeController()], animated: true)
            navigationController?.delegate = nil
        }
    }
}

extension LaunchController: UINavigationControllerDelegate {
    public func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        switch operation {
        case .push:
            return AnimatedTransitioning(navigationController)
        default:
            return nil
        }
    }
}

class AnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitioning {
    private let transitionDuration = 1.0 / sqrt(1000) // 返回一个非常小的值来完成系统的动画
    private let animationDuration = 0.4

    private let navigationController: UINavigationController
    private let navigationBar: UINavigationBar

    init(_ navigationController: UINavigationController) {
        self.navigationController = navigationController
        navigationBar = navigationController.navigationBar
    }

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        transitionDuration
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        guard let toController = transitionContext.viewController(forKey: .to),
              let toView = transitionContext.view(forKey: .to) else {
            transitionContext.completeTransition(false)
            return
        }
        transitionContext.containerView.addSubview(toView)

        navigationBar.alpha = 0
        toView.alpha = 0
        navigationController.view.isUserInteractionEnabled = false
        toController.beginAppearanceTransition(true, animated: true)
        let animator = UIViewPropertyAnimator(duration: animationDuration, curve: .easeInOut) {
            self.navigationBar.alpha = 1
            toView.alpha = 1
        }
        animator.addCompletion { position in
            self.navigationController.view.isUserInteractionEnabled = true
            transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
            toController.endAppearanceTransition()
        }
        animator.startAnimation()
    }
}
